<?php 

Class articleModel extends appModel
{
	/**
	 * 文章排序
	 * @param $val
	 * @param $id
	 * @return 
	 */
	public function ordering($val, $id)
	{
		$result = array('error' => 0, 'content'=>'');
		$sql = "UPDATE ".$this->table('article')." SET ordering='$val' WHERE id='".$id."'";
		$res = $this->query($sql);
		if(!$res)
		{
			$result['error'] = 1;
			$result['content'] = '';
		}
		return $result;
	}

	/**
	 * 获取文章详情
	 * @param $id
	 * @return 
	 */
	public function get_art($id)
	{
		if($id)
		{
		  	$sql = "SELECT a.*,c.name as cat_name FROM ".$this->table('article')." as a LEFT JOIN ".$this->table('category')." as c ON a.cat_id=c.id WHERE a.id = '".$id."'";
		  	$result=$this->getRow($sql);
		  	if($result)
		  	{
		  		$result['cat_name'] = hy_dig_filter($result['cat_name'], 1);
		  		$arr = hy_dig_filter($result['title'], 3);
		  		$arr2 = hy_dig_filter($result['author'], 3);
		  		$arr3 = hy_dig_filter($result['content'], 3);
		  		$arr4 = hy_dig_filter($result['summary'], 3);
		  		$result['en_content'] = stripslashes($arr3[1]);
		  		$result['content'] = stripslashes($arr3[0]);
		  		$result['en_author'] = $arr2[1];
		  		$result['author'] = $arr2[0];
		  		$result['en_title'] = $arr[1];
		  		$result['title'] = $arr[0];
		  		$result['en_summary'] = $arr4[1];
		  		$result['summary'] = $arr4[0];
		  		if(empty($result['flags']))
		  		{
		  			$result['flags'] = 4;
		  		}
		  		else
		  		{
		  			$result['flags'] = explode(',', $result['flags']);
		  		}
		  	//获取相册集
		  		$sql = "SELECT p.* FROM ".$this->table('album_photos')." as p WHERE p.aid = '".$id."'";
		  		$photo = $this->getAll($sql);
		  		foreach($photo as $k => $v)
		  		{
		  			$arr = hy_dig_filter($v['title'], 3);
		  			$photo[$k]['title'] = $arr[0];
		  		}
		  		$result['photo'] = $photo;
				return $result;
		  	}
		  	else
		  	{
				return $result = array();
		  	}
		}
	}
	
	/**
	 * 修改文章信息
	 * @param $info
	 * @param $photo
	 * @param $id
	 * @return 
	 */
	public function update_art($info,$photo,$id)
	{
		if($id)
		{
			$data = yf_set_update($info);
		
			$sql = "UPDATE ".$this->table('article')." SET ".$data." WHERE id='".$id."'";
			
			$res = $this->query($sql);
			
			$aid = $id;
			if($photo)
			{
				$sql="INSERT INTO ".$this->table('album_photos')." (aid,original_pic) VALUES";
				if(is_array($photo))
				{
				    foreach($photo as $k=>$v)
				  {
					$sql.="('$aid','$v'),";
				  }
				}
				else 
				{
					$sql.="('$aid','$photo'),";
				}
				$sql = substr($sql,0,strlen($sql)-1);
				$this->query($sql);
			}
			return $res;
		}
	}
	
	/**
	 * 插入文章信息
	 * @param $info
	 * @param $photo
	 * @return
	 */
	public function create_art($info, $photo)
	{
		if($info)
		{
			$data = yf_set_insert($info);
			$sql = "INSERT INTO ".$this->table('article')." (".$data['key'].") VALUES(".$data['val'].")";
			$this->query($sql);
			$aid = $this->insert_id();
			if(!$aid)
			{
				return false;
			}
			else 
			{
				if(count($photo) > 0)
				{
					$sql="INSERT INTO ".$this->table('album_photos')." (`aid`, `original_pic`) VALUES";
					foreach($photo as $k=>$v)
					{
					    $sql.="('$aid','$v'),";
					}
					$sql = substr($sql,0,strlen($sql)-1);
					$this->query($sql);
				}
			}
			return true;
		}
		return false;
	}
	
	/**
	 * 获取文章列表
	 * @param $sort
	 * @param $page
	 * @param $limit
	 * @return 
	 */
	public function art_list($sort=array(), $page, $limit = 10)
	{
		$listPage = array('data' => '','pageLink' => '');
		$where = ' 1=1';
		if($sort['cat_id'] > 0)
		{
			$where .= ' AND cat_id='.$sort['cat_id'];
		}
		elseif($sort['cat_id'] == -1)//未分类 
		{
			$where .= ' AND c.name is null';
		}
		if($sort['keyword'] != '')
		{
			$where .= " AND title like '%".$sort['keyword']."%'";
		}
		$sql = "SELECT a.id,a.title,a.ordering,c.name,a.created FROM (".$this->table('article')." as a) LEFT JOIN ".$this->table('category')." as c ON a.cat_id=c.id WHERE ".$where." ORDER BY a.id DESC";
		$result = $this->show_page($sql, $page, $limit);
		
		if($result)
		{
			foreach($result['result'] as $k => $v)
			{
				$result['result'][$k]['title'] = hy_dig_filter($v['title'], 1);
				$result['result'][$k]['name'] = hy_dig_filter($v['name'], 1);
				$result['result'][$k]['created'] = date('Y-m-d H:i:s', $v['created']);
			} 
		}
		$listPage['pagelink'] = $result['page'];
	    $listPage['data'] = $result['result'];
		return $listPage;
	}
	
	/**
	 * 获取所有的文章
	 * @param $sort
	 * @return 
	 */
	public function get_all_art($sort = array())
	{
		$where = '1=1';
		$sql = "SELECT a.id,a.title,a.ordering,c.name FROM (".$this->table('article')." as a) LEFT JOIN ".$this->table('category')." as c ON a.cat_id=c.id WHERE ".$where." ORDER BY a.id DESC";
		$result = $this->getAll($sql);
		foreach($result as $k => $v)
		{
			$result[$k]['title'] = hy_dig_filter($v['title'], 1);
			$result[$k]['name'] = hy_dig_filter($v['name'], 1);
			$result[$k]['url'] = 'http://'.$_SERVER['HTTP_HOST'].'/index.php?g=mobile&m=article&a=view&id='.$v['id'];
		}
		return $result;
	}
	
	function get_page_art($cat_id)
	{
		$sql="SELECT id FROM ".$this->table('article')." WHERE cat_id='".$cat_id."'";
		return $this->getOne($sql);
	}
}